package com.example.experiment2;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class ExamTypesAdapter extends
        RecyclerView.Adapter<ExamTypesAdapter.ViewHolder> {
    private static final String TAG = "ExamTypesAdapter";
    private Context context;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View typeView;
        ImageView examTypeIcon;
        TextView examTypeTitle;

        public ViewHolder(View view) {
            super(view);
            typeView = view;
            examTypeIcon = view.findViewById(R.id.exam_type_icon);
            examTypeTitle = view.findViewById(R.id.exam_type_title);
        }
    }

    private List<ExamTypes.ExamType> examTypeList;

    public ExamTypesAdapter(List<ExamTypes.ExamType> examTypeList, Context ctx) {
        if (BuildConfig.DEBUG && examTypeList == null) {
            Log.d(TAG, "ExamTypesAdapter: examTypeList == null");
        }
        this.examTypeList = examTypeList;

        context = ctx;
    }

    @Override
    public int getItemCount() {
        return examTypeList.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.exam_types_item, parent, false);

        final ViewHolder holder = new ViewHolder(view);

        //-----------------------------------------------//
        // 点击事件
        holder.typeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                ExamTypes.ExamType type = examTypeList.get(position);
                Toast.makeText(v.getContext(), "题型：" +
                        type.getType(), Toast.LENGTH_SHORT).show();

                Intent i = new Intent(context, ViewPagerActivity.class);
                context.startActivity(i);
            }
        });

        holder.examTypeIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                ExamTypes.ExamType type = examTypeList.get(position);
                Toast.makeText(v.getContext(), "按在图片上，题型：" +
                        type.getType(), Toast.LENGTH_SHORT).show();
            }
        });
        //-----------------------------------------------//

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ExamTypes.ExamType type = examTypeList.get(position);
        holder.examTypeIcon.setImageResource(type.getImageID());
        holder.examTypeTitle.setText(type.getType());
    }
}